#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# util.py 
#
#  Copyright (c) 2007 INdT (Instituto Nokia de Technologia)
#                2007 Kenneth Rohde Christiansen
#
#  Portions of the code, copyright (c) the Gajim Project (GPL)
#
#  Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA

import pygtk
pygtk.require("2.0")
import gtk
import os
import sys

def rescale_pixbuf(pixbuf, size=48.0):
    """
    Returns pixbuf rescaled to size x size.
    """

    if pixbuf == None:
        return None

    pwidth = pixbuf.get_width()
    pheight = pixbuf.get_height()

    if pwidth < pheight:
        width = size
        height = pheight * (width / pwidth)
    else:
        height = size
        width = pwidth * (height / pheight)

    npixbuf = pixbuf.scale_simple (int(width), int(height), \
            gtk.gdk.INTERP_BILINEAR)

    avatar_x = int(npixbuf.get_width() / 2) - int(size / 2)
    avatar_y = int(npixbuf.get_height() / 2) - int(size / 2)

    avatar = npixbuf.subpixbuf(avatar_x, avatar_y, int(size), int(size))

    return avatar


def pixbuf_from_avatar(avatar):
    """
    Converts an tapioca.Avatar object to a gtk.gdk.Pixbuf.
    """

    if avatar == None:
        return None

    image = avatar.get_image()

    if image == None:
        return None

    mime = avatar.get_mime_type()

    avatar_pixbuf_loader = gtk.gdk.pixbuf_loader_new_with_mime_type(mime)
    avatar_pixbuf_loader.write(image)
    avatar_pixbuf_loader.close()
    avatar_pixbuf = avatar_pixbuf_loader.get_pixbuf()

    return avatar_pixbuf

def get_server_name(account):
    """
    Return the server from the user account id. 
    In other words check if it's an google account.
    """

    server = account.split('@')[1]

    if server == 'gmail.com':
        server = 'talk.google.com'

    return server

def popup_menu_under_button(menu, button, parent_win):
    """
    Pops menu under button, which is in parent_win
    Function gently borrowed from Gajim code (gtkgui_helpers.py). GPL
    """

    window_x1, window_y1 = parent_win.get_origin()

    def position_menu_under_button(menu):
        """
        Embedded function to position the menu under the
        button.
        """

        # inline function, which will not keep refs, when used as CB
        button_x, button_y = button.allocation.x, button.allocation.y

        # now convert them to X11-relative
        window_x, window_y = window_x1, window_y1
        x_coord = window_x + button_x
        y_coord = window_y + button_y

        menu_width, menu_height = menu.size_request()

        ## should we pop down or up?
        if (y_coord + button.allocation.height + menu_height \
            < gtk.gdk.screen_height()):
            # now move the menu below the button
            y_coord += button.allocation.height
        else:
            # now move the menu above the button
            y_coord -= menu_height

        # push_in is True so all the menuitems are always inside screen
        push_in = True

        return (x_coord, y_coord, push_in)

    menu.popup(None, None, position_menu_under_button, 1, 0)


def has_connection_manager(name):
    """
    Function that checks if a given connection manager
    is installed on the system.
    """

    path = sys.exec_prefix + '/bin/telepathy-' + name
    local_path = sys.exec_prefix + '/local/bin/telepathy-' + name

    if os.path.exists(path):
        return os.access(path, os.X_OK)

    if os.path.exists(local_path):
        return os.access(local_path, os.X_OK)

    path = sys.exec_prefix + '/libexec/telepathy-' + name
    local_path = sys.exec_prefix + '/local/libexec/telepathy-' + name

    if os.path.exists(path):
        return os.access(path, os.X_OK)

    if os.path.exists(local_path):
        return os.access(local_path, os.X_OK)

    return False

